package com.example.shengyin.frontController;

import com.example.shengyin.entity.Comment;
import com.example.shengyin.entity.User;
import com.example.shengyin.service.CommentService;
import com.example.shengyin.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("front/comment")
@Api(tags = "CommentFrontController", description = "评论")
public class FrontCommentController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private CommentService service;

//    @ApiOperation("添加评论")
//    @PostMapping("/userInsert")
//    public Result userInsert(Comment comment,HttpServletRequest request){
//        InetAddress address = null;
//        try {
//            address = InetAddress.getLocalHost();
//        } catch (UnknownHostException e) {
//            return Result.error("ip获取失败");
//        }
//        String ipAddress = address.getHostAddress();
//        comment.setIp(ipAddress);
//        String token=request.getHeader("Authorization");
//        User user=(User) redisTemplate.opsForValue().get(token);// 当前登录用户userid
//        comment.setUserid(user.getId());
//        int insert = service.userInsert(comment);
//        if(insert==0)
//            return Result.error("增加失败");
//        return Result.success("增加成功");
//    }



    @PostMapping("/userInsert")
    @ApiOperation("用户新增评论的方法，注意，如果没登录的时候，token为空，这个时候userid为空，前端展现评论的时候请把用户名改为游客")
    public Result userInsert(@ApiParam(name = "articleid",value = "新闻号")@RequestParam Long articleid,
                             @ApiParam(name = "content",value = "内容")@RequestParam String content, HttpServletRequest request) {
        String ip = request.getRemoteAddr();
        String token = request.getHeader("Authorization");

        Comment comment=new Comment();
        comment.setArticleid(articleid);
        comment.setContent(content);
        if (token == null) {
            comment.setUserid(0L);
            comment.setIp(ip);
        } else {
            User user = (User) redisTemplate.opsForValue().get(token);
            comment.setUserid(user.getId());
            comment.setIp(ip);
        }
        int r = service.userInsert(comment);
        if (r > 0)
            return Result.success("增加成功");
        return Result.error("新增失败");
    }

    @GetMapping("/comNum/{articleid}")
    @ApiOperation("按文章的id返回当前文章的所有评论")
    public Result comNum(@PathVariable(value = "articleid") Long articleid)
    {
        List<Comment> comments = service.comNum(articleid);

        return comments==null?Result.error("无评论"):Result.success(comments);
    }

    @GetMapping("/countByarticleid/{articleid}")
    @ApiOperation("传当前文章id返回文章的所有评论的数量")
    public Result countByarticleid(@PathVariable(value = "articleid") Long articleid){
        Long aLong = service.countByarticleid(articleid);
        return Result.success(aLong);
    }


    @ApiOperation("删除单个评论，传评论id过来")
    @DeleteMapping("front/delete/{id}")
    public Result delete(@PathVariable(value = "id")Long id, HttpServletRequest request)
    {
        Long uid = service.selectUseridById(id);// 传过来的id的userid
        String token=request.getHeader("Authorization");
        User user=(User) redisTemplate.opsForValue().get(token);// 当前登录用户userid
        if (!uid.equals(user.getId())){
            return Result.success("不可以删除别人的评论");
        }

        int delete = service.delete(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }

}
